home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: String assignments, please help (beginner)
- Date: 9 Apr 1996 09:10 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <9APR199609100931@erich.triumf.ca>
- References: <4ke05g$27q@soap.news.pipex.net>
- NNTP-Posting-Host: ftp.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4ke05g$27q@soap.news.pipex.net>, tone@dial.pipex.com (Tone) writes...
- >I've just been learning C for the last few weeks and have come across
- >a problem with assigning strings, which I will detail below.
- >(Sorry the posting is so long, I'm just trying to make sure you know
- >exactly what my problem is):
- >
- >I was originally informed that the following was valid:
- >
- > char string1[30];
- > string1 = "Test string";
-
- NO, it's not valid.
- >
- >This gave me an error on the second line although when declaring I am
- >able to use:
- >
- > char string1[30] = "Test string";
-
- Yes, this is a special case when _declaring_ an array.
-
- >so I arrived at the following:
- >
- > char *string1[30];
- > *string1 = "Test string";
-
- This declares an array of 30 pointers-to-char, and sets string1[0] to point to
- the string literal "Test String". (Actually, I'm not certain that the
- assignment is valid...)
-
- To assign (or copy) strings, you need to use the strcpy() function. If you use
- '=' (or '==') with strings, you are dealing with the value of the pointers to
- the strings, not with the string contents.
-
-
- >The only other way I know of assigning a string is to create a loop
- >(e.g. a "for" loop) and assign each character of the string
- >individually and manually add the '\0' at the end. This seems
- >extremely laborious and I'm sure it can't be the best way. (Also it
- >may give similar "unexpected results"?)
-
- Well, actually it is the only way. Fortunately, the compiler vendors (and C
- standard writers) have taken pity on us, and supply many standard functions to
- handle strings - most have names starting with "str" - have a look in your
- library documentation for full details.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
- or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
-
-
-
-
-
-
-
-
-
-
-